package com.emip.servlet.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.emip.bean.AlterLogBean;
import com.emip.bean.SecondStoreBean;
import com.emip.bean.SiteItemBean;
import com.emip.pojo.Report;
import com.emip.pojo.Table;
import com.emip.service.SecondStoreService;
import com.emip.util.TableFormat;


@Controller
@RequestMapping(value="/data/second")
public class SecondDataAction extends DataManage {

	@Autowired
	SecondStoreService secondStoreService;
	
	@RequestMapping(value="/show")
	public ModelAndView index() {
		return new ModelAndView("/data/data", setDefaultParams(mmap));
	}
	
	@RequestMapping(value="/search", method=RequestMethod.POST)
	@ResponseBody
	public  Table<Map<String, Object>> search(
			@RequestParam("startDate") String startDate, 
			@RequestParam("startHour") String startHour,
			@RequestParam("endDate") String endDate,
			@RequestParam("endHour") String endHour,
			@RequestParam("siteId") String siteId,
			@RequestParam("page") int page,
			@RequestParam("rows") int size) {
		startDate += " " + startHour + ":00:00";
		endDate += " " + endHour + ":59:59";
		String[] itemIds = getItemIds(siteId);
		List<SecondStoreBean> result = secondStoreService.getByTimespan(startDate, endDate, itemIds, siteId, page, size);
		long records = secondStoreService.getCount(startDate, endDate, itemIds, siteId);
		long totalPages = records / size + (records % size == 0 ? 0 : 1);
		Report report = formatReportBody(result);
		return new TableFormat<Map<String, Object>>().formTable(totalPages, page, records, report.getBodys());
	}
	
//	@RequestMapping(value="/update", method=RequestMethod.POST)
//	@ResponseBody
//	public String update(
//			@RequestParam("id") String time,
//			@RequestParam("siteId") String siteId,
//			HttpServletRequest request) {
//		List<SecondStoreBean> stores = secondStoreService.getByTimespan(time, time, null, siteId, 1, 100);
//		for (int i = 0; i < stores.size(); i++) {
//			SecondStoreBean store = stores.get(i);
//			String data = (String) request.getParameter(store.getItemID());
//			if (data == null || data.equals("")) continue;
//			store.setValue(data);
//			setFlag(store);// 处理Flag
//			secondStoreService.merge(store);
//		}
//		return "success";
//	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(
			@RequestParam("id") String time,
			@RequestParam("siteId") String siteId,
			HttpServletRequest request) throws ParseException {
		
		List<SiteItemBean> siteItems = siteItemService.getSiteItemBySiteID(siteId);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SecondStoreBean store = new SecondStoreBean();
		Date date = sf.parse(time);
		for (int i = 0; i < siteItems.size(); i++) {
			SiteItemBean siteItem = siteItems.get(i);
			String[] itemIds = new String[]{siteItem.getItem().getId()};
			String data = (String) request.getParameter(itemIds[0]);
			if (data == null || data.equals("")) continue;
			List<SecondStoreBean> stores = secondStoreService.getByTimespan(time, time, itemIds, siteId, 1, 100);
			AlterLogBean log = new AlterLogBean();
			log.setGenTime(new Date());
			log.setItem(siteItem.getItem());
			log.setSite(siteItem.getSite());
			log.setType("Second");
			if (stores == null || stores.size() == 0) {
				store.setGenTime(date);
				store.setItem(siteItem.getItem());
				store.setSite(siteItem.getSite());
			} else {
				store = stores.get(0);
				log.setOldvalue(store.getValue());
			}
			
			store.setValue(data);
			setFlag(store);// 处理Flag
			secondStoreService.merge(store);
			log.setNewvalue(data);
			log.setFlag("F");
			log.setDate(store.getTimeLabel());
			alterLogService.merge(log);
		}
		return store.getFlag();
	}
}
